﻿using System;
using System.Windows.Controls;
using System.Windows;

namespace PasswordProvider.View.Library
{
    /// <summary>
    /// Bug in WPF 3.5 makes it impossible to bind to the RadioButton IsChecked property. The binding holds for one click
    /// but is lost after that. This is a known issue that apparently is fixed in .Net 4.0.
    /// </summary>
    class BindableRadioButton : RadioButton
    {
        public BindableRadioButton()
        {
            Checked += delegate { IsChecked = true; };
            Unchecked += delegate { IsChecked = false; };
        }

        public static new readonly DependencyProperty IsCheckedProperty =
            DependencyProperty.Register("IsChecked", typeof(bool), typeof(BindableRadioButton),
            new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.OverridesInheritanceBehavior | FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,
                new PropertyChangedCallback(
                    (s, e) =>
                    {
                        (s as RadioButton).IsChecked = (bool)e.NewValue;
                    })));

        public new bool IsChecked
        {
            get { return (bool)GetValue(IsCheckedProperty); }
            set { SetValue(IsCheckedProperty, value); }
        }
    }
}
